﻿package com.ghost.view.content_menu
{
	import com.ghost.event.EasyContextMenuEvent;
	
	import flash.events.*;
	import flash.ui.*;
	
	
	
	/**
	 * ... 自定义菜单项数据 ...
	 * @author Jerry
	 * @version 0.2.0
	 */
	public class EasyContextMenuItem extends EventDispatcher
	{	
		/** 回调方法菜单项 */
		public static const MENU_ITEM_CALLBACK:String = "menuItemCallback";
		
		/** 链接菜单项 */
		public static const MENU_ITEM_LINK:String = "menuItemLink";
		
		/** 回调方法 */
		public var callback:Function;
		
		/** 链接 */
		public var link:String;
		
		/** 窗口呈现方式 */
		public var window:String;
		
		/** @private 菜单项 */
		private var _item:ContextMenuItem;
		
		/** @private 菜单项类型 */
		private var _type:String;
		
		/**
		 * 构造函数
		 * @param	item 一个ContextMenuItem对象，要创建的菜单对象引用
		 * @param	type 一个字符串，表示菜单项的类型
		 */
		public function EasyContextMenuItem(item:ContextMenuItem, type:String = MENU_ITEM_CALLBACK) 
		{
			_item = item;
			_type = type;
			if (_item)
				_item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
		}
		
		/** 获取菜单项 */
		public function get item():ContextMenuItem { return _item; }
		
		/**
		 * 销毁对象方法
		 */
		public function dispose():void 
		{
			if (_item && _item.hasEventListener(ContextMenuEvent.MENU_ITEM_SELECT)) 
				_item.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
			callback = null;
			link = null;
			window = null;
			_type = null;
		}
		
		/**
		 * 菜单项选择事件调度
		 * @param	e 一个ContextMenuEvent事件对象
		 */
		private function menuItemSelectHandler(e:ContextMenuEvent):void 
		{
			var event:EasyContextMenuEvent = new EasyContextMenuEvent(EasyContextMenuEvent.MENU_ITEM_SELECT);
			event.itemType = _type;
			event.callback = callback;
			event.link = link;
			event.window = window;
			dispatchEvent(event);
		}
	}

}